package com.deluxelx.rmo.essay.domain.rule.engine.impl;

import cn.hutool.core.util.ObjectUtil;
import com.deluxelx.rmo.essay.domain.rule.engine.EngineBase;
import com.deluxelx.rmo.essay.domain.rule.repository.IRuleTreeRepository;
import com.deluxelx.rmo.essay.model.aggregates.TreeRuleRich;
import com.deluxelx.rmo.essay.model.bo.TreeNode;
import com.deluxelx.rmo.essay.model.req.DecisionMakingReq;
import com.deluxelx.rmo.essay.model.res.DecisionMakingRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("ruleEngineHandle")
public class RuleEngineHandle extends EngineBase {

    @Resource
    private IRuleTreeRepository ruleTreeRepository;

    @Override
    public DecisionMakingRes process(DecisionMakingReq decisionMakingReq) {
        // 决策规则树
        TreeRuleRich treeRuleRich = ruleTreeRepository.queryTreeRuleRich(decisionMakingReq.getTreeId());
        if (ObjectUtil.isNull(treeRuleRich)) {
            throw new RuntimeException("Tree Rule is null!");
        }
        // TODO 得到决策后最终的结果节点（包含文章ID）
        TreeNode treeNode = this.engineDecisionMaker(treeRuleRich, decisionMakingReq);

        return new DecisionMakingRes(Boolean.TRUE, decisionMakingReq.getUserId(), decisionMakingReq.getTreeId(), treeNode.getTreeNodeId(), treeNode.getNodeValue());
    }
}
